package com.jh3502.vivo.function.travelpages.approve;

import android.os.Bundle;

import com.google.gson.Gson;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseFragment;
import com.jh3502.vivo.function.todo.TodoListBean;
import com.jh3502.vivo.function.tododetail.travel.TodoTravelActivity;
import com.jh3502.vivo.function.travelpages.TravelListPresenter;
import com.jh3502.vivo.utils.Constant;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @author harisucici
 * @version 2018/11/28 14:15
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class TravelApproveFragment extends BaseFragment<TravelListPresenter.TravelListView, TravelListPresenter> implements TravelListPresenter.TravelListView {

    @BindView(R.id.travel_recycler)
    MyRecyclerView travelRecycler;
    List<TodoListBean> dataList = new ArrayList<>(2);
    List<Map<String, Object>> bodyList = new ArrayList<>(2);
    private TravelApproveAdapter mAdapter;
    private int p;

    @Override
    public TravelListPresenter initPresenter() {
        return new TravelListPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.fragment_travel;
    }

    @Override
    protected void initView() {
        mAdapter = new TravelApproveAdapter(mContext, dataList);
        travelRecycler.setAdapter(mAdapter);
        mPresenter.setTravelListView(this);
        mAdapter.setOnItemClickListener(s -> {
            Bundle bundle = new Bundle();
            bundle.putInt("id", dataList.get(s).getId());
            bundle.putInt("source", Constant.TRAVEL_APPROVE);
            bundle.putString("bean", new Gson().toJson(dataList.get(s)));
            bundle.putString("businessNo", dataList.get(s).getBusinessNo());
            Utils.getInstance().startActivity(mActivity, TodoTravelActivity.class, bundle);
        });

        mAdapter.setButtonSetOnclickSubmit(position -> {
            bodyList.clear();
            bodyList.add(doApprove(1, dataList.get(position)));
            mPresenter.toAgreeForAll(bodyList);
        });

        mAdapter.setButtonSetOnclickCancel(position -> {
            bodyList.clear();
            bodyList.add(doApprove(2, dataList.get(position)));
            mPresenter.toAgreeForAll(bodyList);
        });
        mAdapter.setButtonSetOnclickDelete(position -> {
            p = position;
            Map<String, Object> body = new HashMap<>(2);
            body.put("id", dataList.get(position).getAuditId());
            body.put("type", Constant.TYPE_TRAVEL);
            mPresenter.toDeleteAudit(body);
        });
    }

    private Map<String, Object> doApprove(int state, TodoListBean TodoListBean) {
        Map<String, Object> body = new HashMap<>(2);
        body.put("userId", TodoListBean.getUserId());
        body.put("type", Constant.TYPE_TRAVEL);
        body.put("state", state);
        body.put("destination", TodoListBean.getDestination());
        body.put("businessNo", TodoListBean.getBusinessNo());
        body.put("id", TodoListBean.getId());
        body.put("flowNo", TodoListBean.getFlowNo());
        body.put("currentNodeNo", TodoListBean.getCurrentNodeNo());
        return body;
    }

    @Override
    protected void initData() {


    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {
        Map<String, Object> body = new HashMap<>(2);
        body.put("type", Constant.TYPE_TRAVEL);
        mPresenter.toFindAuditTask(body);
    }

    @Override
    protected void doOnVisible() {

    }

    @Override
    protected void doOnInvisible() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);
    }

    @Override
    public void requestFailure(Throwable t) {

    }


    @Override
    public void getMyTasklSuccess(List<TodoListBean> datas) {


    }

    @Override
    public void findAuditTaskSuccess(List<TodoListBean> datas) {
        dataList.clear();
        dataList.addAll(datas);
        mAdapter.notifyDataSetChanged();

    }

    @Override
    public void agreeForAllSuccess() {
        showShortToast("审批成功");
        Map<String, Object> body = new HashMap<>(2);
        body.put("type", Constant.TYPE_TRAVEL);
        mPresenter.toFindAuditTask(body);
    }

    @Override
    public void deleteAuditSuccess() {
        showShortToast("删除成功");
        dataList.remove(p);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void deleteTaskSuccess() {

    }
}
